﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Sonce.Editor.Tools
{
    public class TextTool : IEditorTool
    {
        #region Members
        private Color textColor = Colors.White;
        private TextTypes textType = TextTypes.SingleLine;
        #endregion

        #region Constructors
        public TextTool() : this(string.Empty)
        {
        }

        public TextTool(string text)
        {
            this.FontSize = 10.0;
            this.Text = text;
        }
        #endregion

        #region Properties
        public string Text
        {
            get;
            set;
        }

        public Color TextColor
        {
            get
            {
                return this.textColor;
            }
            set
            {
                this.textColor = value;
            }
        }

        public TextTypes TextType
        {
            get
            {
                return this.textType;
            }
            set
            {
                this.textType = value;
            }
        }

        public double FontSize
        {
            get;
            set;
        }

        public FontStyle FontStyle
        {
            get;
            set;
        }

        public FontWeight FontWeight
        {
            get;
            set;
        }
        #endregion

        #region Methods
        protected Canvas Render()
        {
            if (string.IsNullOrEmpty(this.Text))
            {
                this.Text = "(enter text)";
            }

            TextBlock label = new TextBlock();
            label.Foreground = new SolidColorBrush(this.TextColor);
            label.FontSize = this.FontSize;
            //label.FontStyle = this.FontStyle;
            //label.FontWeight = this.FontWeight;
            //label.TextAlignment;
            //label.TextDecorations;
            //label.TextWrapping;
            //label.Width;
            //label.Height;
            label.Text = this.Text;
            label.Margin = new Thickness(0.2 * this.FontSize, -1.5 * this.FontSize, 0, 0);

            Canvas canvas = new Canvas();
            canvas.Children.Add(label);
            return canvas;
        }
        #endregion

        #region ITool Members
        public Editor Editor
        {
            get;
            set;
        }

        public bool KeyDown(Key key)
        {
            return true;
        }

        public bool KeyUp(Key key)
        {
            return true;
        }

        public bool MouseDown(Point position)
        {
            this.Editor.LayerPreview.Draw(Render(), position);
            return true;
        }

        public bool MouseUp(Point position)
        {
            this.Editor.LayerPreview.Clear();
            this.Editor.SelectionManager.UnselectAll();
            this.Editor.LayerWorking.Draw(Render(), position);
            return true;
        }

        public bool MouseMove(Point position, bool isMouseDown)
        {
            if (isMouseDown)
            {
                this.Editor.LayerPreview.Draw(Render(), position);
            }
            return true;
        }

        public bool MouseEnter()
        {
            return false;
        }

        public bool MouseLeave()
        {
            return false;
        }

        public bool MouseWheel(int delta)
        {
            return false;
        }
        #endregion
    }
}
